/**
 * 评论审核
 * @author wangyixuan
 */
var bcSelectData=null;
var mydatagrid=null;
var indexPageViewDialog=null;
var lookContextData=null;
var lookContextDialog="";
layui.use(["form","laypage","layer",'table'],function(){
    var form=layui.form;
    var layer=layui.layer;
    var table = layui.table; //表格
    form.on('select', function(data){
        // console.log(data.elem); //得到select原始DOM对象
        //console.log(data.value); //得到被选中的值]
        // console.log(data.othis); //得到美化后的DOM对象
        form.render('select');
    });

    layui.fsButton.test3 = function(elem,data,datagrid){
        if(data.length==0){
            layer.alert("请先选择要操作的数据",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        mydatagrid=datagrid;
        var length=data.length;
        if(length>1){
            layer.alert("请选择一条回复记录查看上下文",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        //评论id
        var answ_id = data[0].answ_id;
        //审核状态
        var check_flg = data[0].answ_check_flg;
        var check_flg_Chinese = "";
        //回复内容
        var answ_dtl = data[0].answ_dtl;
        //星级
        var productStar = data[0].productStar;
        //标签
        var answ_tagArr = new Array();
        if(data[0].productTag!=null&&data[0].productTag!=undefined&&data[0].productTag!=""){
            var answ_tag = data[0].productTag.split(",");
            for(var i=0;i<answ_tag.length;i++){
                answ_tagArr.push(answ_tag[i]);
            }
        }
        var answ_dtl_qianmian = answ_dtl.split(":")[0]+":";
        var answ_dtl_houmian = answ_dtl.split(":")[1];


        lookContextDialog=layer.open({
            type: 1 //此处以iframe举例
            ,title: '查看上下文'
            ,area: ['673px', '438px']
            ,fixed:false
            ,scrollbar: false
            /* ,btn: [ '返回']*/
            /* ,yes: function(){
                 save();
             }*/
            ,btn2: function(){

            }
            ,shade: 0
            ,maxmin: false
            ,content:$("#lookContextDiv")
            ,end:function(){
                location.reload();　　//layer.open关闭刷新
            }
        });

        $(" .layui-layer-content").css('overflow-x','hidden');
        lookContextData= new Vue({
            el: '#lookContextAppDiv',
            data: {
                // answList: [{username:"用户1"},{dtl:"123"}],
                // asdf:123,
                answList: [
                    { id: 0, text: '蔬菜' },
                    { id: 1, text: '奶酪' },
                    { id: 2, text: '随便其它什么人吃的东西' }
                ],
                answ_dtl_qianmian:answ_dtl_qianmian,
                answ_dtl_houmian:answ_dtl_houmian,
                answ_tagArr:answ_tagArr,
                check_flg:check_flg,
                answ_id:answ_id
                // answCheckFlg:answCheckFlg
            },

        })
        //星星的个数（数字类型）
        var starNums = parseInt(productStar);
        //如果没有选择星星，默认为三颗星
        var starIcon = "<img src=\"../../images/star-on.png\" alt=\"1\" class=\"abc\" title=\"bad\">\n" +
            "<img src=\"../../images/star-on.png\" alt=\"2\" class=\"abc\" title=\"poor\">\n" +
            "<img src=\"../../images/star-on.png\" alt=\"3\" class=\"abc\" title=\"regular\">\n" +
            "<img src=\"../../images/star-off.png\" alt=\"4\" class=\"abc\" title=\"good\">\n" +
            "<img src=\"../../images/star-off.png\" alt=\"5\" class=\"abc\" title=\"gorgeous\">";

        //亮着的星星
        var onStar = "";
        for(var i=0;i<starNums;i++){
            onStar += "<img src=\"../../images/star-on.png\" alt=\"1\" class=\"abc\" title=\"bad\">\n";
        }
        //没有亮的星星
        var offStar = "";
        for(var i=starNums;i<5;i++){
            offStar+="<img src=\"../../images/star-off.png\" alt=\"1\" class=\"abc\" title=\"bad\">\n"
        }
        //所有的星星
        starIcon = onStar+offStar;
        var starIbody = document.getElementById("star-icon");
        starIbody.innerHTML=starIcon;
    }

})
/**
 * 审核操作处理
 * @param id 主键，多个以逗号分隔
 * @param status状态 Y 标识审核通过 N 标识审核驳回
 * @author wangyixuan
 */
function ajaxSuccess(id,whatToDo,datagrid){
    $.ajax({
        url: "/eqProduct/updatepruductAnswer", //请求的url地址
        dataType: "json", //返回格式为json
        async: true, //请求是否异步，默认为异步，这也是ajax重要特性
        data: {
            "answ_id": id,
            "whatToDo":whatToDo
        }, //参数值
        type: "POST", //请求方式
        beforeSend: function() {
            //请求前的处理
        },
        success: function(req) {
            //请求成功时处理
            if(datagrid){
                datagrid.reload();
            }else{
                layui.fsDatagrid.datagrid.reload();
            }
            if(lookContextDialog){
                layer.close(lookContextDialog);
            }

        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {
            //请求出错处理
        }
    });
}

/**
 * 保存
 * @author wangyixuan
 */
function save(){
    var quesIds="";
    var hopgOrders="";
    var isNotSelectHopgOrder=false;
    // 为所有选中的复选框执行函数，
    var length=$("input[name='hopgFlg']:checked").length;
    if(length!=3){
        layer.alert("请选择3个首页展示问题",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $("input[name='hopgFlg']:checked").each(function(index,element){
        var id=$(element).attr('data-id');
        var quesId=$(element).attr('data-quesId');

        var hopgOrder=$("#hopgOrder"+id).val();
        if(''==hopgOrder){
            isNotSelectHopgOrder=true;
        }
        if(index<length-1){
            quesIds+=quesId+",";
            hopgOrders+=hopgOrder+",";
        }else{
            quesIds+=quesId;
            hopgOrders+=hopgOrder;
        }
    })
    if(isNotSelectHopgOrder){
        layer.alert("请为首页展示问题配置顺序",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $.ajax({
        url: "/skQuesDtl/setIndexPageView.shtml", //请求的url地址
        dataType: "json", //返回格式为json
        async: true, //请求是否异步，默认为异步，这也是ajax重要特性
        data: {
            "quesId": quesIds,
            "hopgOrder":hopgOrders
        }, //参数值
        type: "POST", //请求方式
        beforeSend: function() {
            //请求前的处理
        },
        success: function(req) {
            //请求成功时处理
            layer.closeAll();
            mydatagrid.reload();

        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {
            //请求出错处理
        }
    });
}

/**
 * 查看上下文
 * @author wangyixuan
 */
function preView(){
    var viewData=[];
    var quesIds="";
    var hopgOrders="";
    var isNotSelectHopgOrder=false;
    // 为所有选中的复选框执行函数，
    var length=$("input[name='hopgFlg']:checked").length;
    if(length!=3){
        layer.alert("请选择3个首页展示问题",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $("input[name='hopgFlg']:checked").each(function(index,element){
        var id=$(element).attr('data-id');
        // var quesId=$(element).attr('data-quesId');
        var quesId=$(element).parent().prev().html();
        var hopgOrder=$("#hopgOrder"+id).val();
        if(''==hopgOrder){
            isNotSelectHopgOrder=true;
        }
        if(index<length-1){
            quesIds+=quesId+",";
            hopgOrders+=hopgOrder+",";
        }else{
            quesIds+=quesId;
            hopgOrders+=hopgOrder;
        }
        viewData.push({
            name:quesId,
            num:hopgOrder
        })
    })
    viewData = viewData.sort(compare("num"));
    if(isNotSelectHopgOrder){
        layer.alert("请为首页展示问题配置顺序",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    var indexPre=layer.open({
        type: 1 //此处以iframe举例
        ,title: '预览'
        ,area: ['400px', '300px']
        ,btn: ['确认保存', '重新配置']
        ,yes: function(){
            save();
        }
        ,btn2: function(){
            layer.close(indexPre);
        }
        ,shade: 0
        ,maxmin: false
        ,content:$("#preViewDiv")
        ,end:function(){
            layer.close(indexPre);
        }

    });
    new Vue({
        el: '#preViewDiv',
        data: {
            preViewData: viewData
        }
    })
}
// 指定排序的比较函数
function compare(property){
    return function(obj1,obj2){
        var value1 = obj1[property];
        var value2 = obj2[property];
        return value1 - value2;     // 升序
    }
}

